package com.JFapp.oss;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.STSAssumeRoleSessionCredentialsProvider;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Data
@Component
@ConfigurationProperties(prefix = "oss")
public class uploadOSS {
    private MultipartFile img;
    private String region;
    private String accessKeyId;
    private String accessKeySecret;
    private String roleArn;


    public String uploadOSS(MultipartFile imgFile, String filePath){
        // 使用代码嵌入的RAM用户的访问密和RAM角色的RamRoleArn配置访问凭证。
        STSAssumeRoleSessionCredentialsProvider credentialsProvider;
        try {
            credentialsProvider = CredentialsProviderFactory
                    .newSTSAssumeRoleSessionCredentialsProvider(
                            region,
                            accessKeyId,
                            accessKeySecret,
                            roleArn
                    );
        } catch (com.aliyuncs.exceptions.ClientException e) {
            throw new RuntimeException(e);
        }
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
        // 填写Bucket名称，例如examplebucket。Bucket名称在OSS范围内必须全局唯一。
        String bucketName = "qq-blog";
        String ObjectName =filePath;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
        try {
            InputStream inputStream=imgFile.getInputStream();
            ossClient.putObject(bucketName,ObjectName,inputStream);
            return "http://10309shizuidiaode.top/"+ObjectName;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return  "www";
    }
}
